package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.entity.RoomInfo;
import com.contest.quartz.QuartzManager;
import com.contest.quartz.data.ScheduleJob;
import com.contest.util.DateUtil;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import com.contest.ws.WsGameNumber;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import com.contest.entity.GameNumbersRule;
import com.contest.service.GameNumbersRuleService;

import javax.servlet.http.HttpServletRequest;


/**
 *  
 *
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-05-17 14:57:27
 */

@Controller
@RequestMapping("/gameNumbersRule")
public class GameNumbersRuleController {

    @Autowired
    GameNumbersRuleService gameNumbersRuleService;
    @Autowired
    IdWorker idWorker;


    @RequestMapping("/client/buyNumber")
    @ResponseBody
    public WsGameNumber buyNumber(HttpServletRequest request , WsGameNumber wsGameNumber) {

        wsGameNumber = gameNumbersRuleService.buyNumber(wsGameNumber);
        return wsGameNumber;
    }


    @RequestMapping("/client/getCurrentDateGameNumberInfos")
    @ResponseBody
    public Map getCurrentDateGameNumberInfos(HttpServletRequest request) {

        Map msg = new HashMap();
        gameNumbersRuleService.getCurrentDateGameNumberInfos(request , msg);
        return msg;
    }


    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<GameNumbersRule> mPage = new Page<GameNumbersRule>(page , rows);
        param.remove("page");
        param.remove("rows");
        Wrapper<GameNumbersRule> w = new EntityWrapper<>();
        Set<String> keys = param.keySet();
        for (String key : keys) {
            Object value = param.get(key);
            if (value != null && !"".equals(value)) {
                w.like(key, value.toString());
            }
        }
        w.orderBy("fd_date" , false);
        mPage = gameNumbersRuleService.selectPage(mPage , w);
        try {
            List<ScheduleJob> listJobs = QuartzManager.getInstance().getAllJob();
            System.out.println("==============================jobs:" + listJobs.size());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(GameNumbersRule gameNumbersRule) {
        return gameNumbersRuleService.save(gameNumbersRule);
    }


    @PostMapping("/update")
    @ResponseBody
    public R update(GameNumbersRule gameNumbersRule) {

        return gameNumbersRuleService.updateRule(gameNumbersRule);

    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        GameNumbersRule gameNumbersRule = gameNumbersRuleService.selectById(fdId);
        model.addAttribute("gameNumbersRule", gameNumbersRule);
        return "gameNumbersRule/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {

        return gameNumbersRuleService.deleteRule(ids);

    }


}
